package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

/**
 * @Author 王加鑫
 * @Date 2021/5/4 20:40
 * @Version 1.0
 */
public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    Scanner sc = new Scanner(System.in);
    /*
    搭建老师框架的基本框架
     */
    public void start() {
        while (true){
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String t = sc.next();
            switch (t){
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("退出");
                    //结束当前系统,不能结束虚拟机
                    return;
                default:
                    System.out.println("您的输入有误,请重新输入:");
            }
        }
    }

    /*
    contoller层只负责给客户展示数据
    不能直接操作数据即数组里的元素
     */
    private void updateTeacher() {
        System.out.println("请输入您要修改的老师id:");
        String tId;
        while (true){
            tId = sc.next();
            //判断tId是否在数组中存在
            int exists = teacherService.isExists(tId);
            //如果在数组中不存在,就不通过
            if(exists == -1){
                System.out.println("您输入的工号不存在,请重新输入:");

            }else { //如果存在,继续下面的修改操作
                break;
            }
        }

        Teacher teacher = InfoTeacher(tId);
        teacherService.updateTeacher(tId,teacher);
        System.out.println("修改成功!");

    }

    public Teacher InfoTeacher(String tId) {
        System.out.println("请输入老师姓名：");
        String tName = sc.next();
        System.out.println("请输入老师年龄：");
        String tAge = sc.next();
        System.out.println("请输入老师生日：");
        String tBirth = sc.next();
        Teacher teacher = new Teacher();
        teacher.setId(tId);
        teacher.setName(tName);
        teacher.setAge(tAge);
        teacher.setBirth(tBirth);
        return teacher;
    }

    private void deleteTeacher() {
        System.out.println("请输入您要删除的老师id:");
        String tId = sc.next();
        /*
        判断学生id在数组中是否存在
        存在就删除
        不存在就提示错误
         */
        boolean deleteTeacher = teacherService.deleteTeacher(tId);
        if(deleteTeacher){
            System.out.println("删除成功！");
        }else {
            System.out.println("系统中不存在该用户，请重新输入！！！");
        }

    }

    /*
    展示老师列表
     */
    private void findAllTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        //当数组元素全部为null时
        if(teachers == null){
            System.out.println("系统信息为空,请添加数据后再进行查询");
            return;
        }

        System.out.println("老师工号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            //只提取不为null的元素
            if(teachers[i] != null){
                System.out.println(teachers[i].getId() + "\t\t\t" + teachers[i].getName()
                        + "\t" + teachers[i].getAge() + "\t\t" + teachers[i].getBirth());
            }
        }
    }



    /*
    添加老师
     */
    private void addTeacher() {
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请输入老师id:");
            String tId = sc.next();
        /*
        在输入id的时候需要校验在数组中是否存在
         */
            int index = teacherService.isExists(tId);
            if(index != -1){
                System.out.println("工号已被占用,请重新输入!");
            }else {
                System.out.println("请输入老师name:");
                String tName = sc.next();
                System.out.println("请输入老师age:");
                String tAge = sc.next();
                System.out.println("请输入老师birth:");
                String tBirth = sc.next();
                Teacher teacher = new Teacher();
                teacher.setId(tId);
                teacher.setName(tName);
                teacher.setAge(tAge);
                teacher.setBirth(tBirth);
                //指挥service层添加学生
                boolean flag = teacherService.addTeacher(teacher);
                if(flag){
                    System.out.println("添加成功!");
                }else {
                    System.out.println("添加失败");
                }
                break;
            }
        }
    }
}
